package bll

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/model"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
	"github.com/google/wire"
)

// GroupVideoSet 注入GroupVideo
var GroupVideoSet = wire.NewSet(wire.Struct(new(GroupVideo), "*"))

// GroupVideo 群视频
type GroupVideo struct {
	GroupVideoModel *model.GroupVideo
	LikeModel *model.Like
}

// Query 查询数据
func (a *GroupVideo) Query(ctx context.Context, params schema.GroupVideoQueryParam, opts ...schema.GroupVideoQueryOptions) (*schema.GroupVideoQueryResult, error) {
	return a.GroupVideoModel.Query(ctx, params, opts...)
}

// GetFirst 查询数据
func (a *GroupVideo) GetFirst(ctx context.Context, params schema.GroupVideoQueryParam, opts ...schema.GroupVideoQueryOptions) (*schema.GroupVideo, error) {
	item, err := a.GroupVideoModel.GetFirst(ctx, params, opts...)
	if err != nil {
		return nil, err
	}
	if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Get 查询指定数据
func (a *GroupVideo) Get(ctx context.Context, id string, opts ...schema.GroupVideoQueryOptions) (*schema.GroupVideo, error) {
	item, err := a.GroupVideoModel.Get(ctx, id, opts...)
	if err != nil {
		return nil, err
	} else if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Create 创建数据
func (a *GroupVideo) Create(ctx context.Context, item schema.GroupVideo) (*schema.IDResult, error) {

	item.ID = uuid.NewID()
	err := a.GroupVideoModel.Create(ctx, item)
	if err != nil {
		return nil, err
	}

	return schema.NewIDResult(item.ID), nil
}

// Update 更新数据
func (a *GroupVideo) Update(ctx context.Context, id string, item schema.GroupVideo) error {
	_, err := a.Get(ctx, id)
	if err != nil {
		return err
	}

	return a.GroupVideoModel.Update(ctx, id, item)
}

// Delete 删除数据
func (a *GroupVideo) Delete(ctx context.Context, id string) error {
	item, err := a.GroupVideoModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.GroupVideoModel.Delete(ctx, id)
}
